package com.me.study.rabbitmq.controller;

import com.me.study.rabbitmq.config.GlobalEventPublisher;
import com.me.study.rabbitmq.rabbit.ConfirmReturn;
import com.me.study.rabbitmq.rabbit.MessageReturn;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author : JCccc
 * @CreateTime : 2019/9/3
 * @Description :
 **/
@RestController
public class SendMessageController implements InitializingBean {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
    @Autowired
    ConfirmReturn confirmReturn;
    @Autowired
    MessageReturn messageReturn;
    @Autowired
    private GlobalEventPublisher globalEventPublisher;


    @Override
    public void afterPropertiesSet() throws Exception {
        rabbitTemplate.setConfirmCallback(confirmReturn);
        rabbitTemplate.setReturnsCallback(messageReturn);
    }


    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId ",messageId);
        map.put("messageData ",messageData);
        map.put("createTime ",createTime);
        Message<Map<String, Object>> build = MessageBuilder.withPayload(map).build();
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        globalEventPublisher.testDirectExchangeOutput().send(build);
        return "ok";
    }

    @GetMapping("/sendDirectMessage2")
    public String sendDirectMessage2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId ",messageId);
        map.put("messageData ",messageData);
        map.put("createTime ",createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestNNNDirectRouting", map);
        return "ok";
    }

    @GetMapping("/sendTopicMessage1")
    public String sendTopicMessage1() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: M A N ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        Message<Map<String, Object>> build = MessageBuilder.withPayload(manMap).build();
        globalEventPublisher.topicexchangeAllOutput().send(build);
        return "ok";
    }

    @GetMapping("/sendTopicMessage2")
    public String sendTopicMessage2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: woman is all ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        Message<Map<String, Object>> build = MessageBuilder.withPayload(womanMap).build();
        globalEventPublisher.topicexchangeManOutput().send(build);
        return "ok";
    }

    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        Message<Map<String, Object>> build = MessageBuilder.withPayload(map).build();
        globalEventPublisher.fanoutexchangeOutput().send(build);
        return "ok";
    }

    @GetMapping("/sendBackupMessage")
    public String sendBackupMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        Message<Map<String, Object>> build = MessageBuilder.withPayload(map).setHeader("priority",222).build();

        globalEventPublisher.directExchangeOutput().send(build);
        return "ok";
    }


}